package com.zhangxun.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhangxun.domain.Category;
import com.zhangxun.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "商品分类接口管理")
@RestController
@RequestMapping("/prod/category")
public class ProdCategoryController {

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/table")
    @ApiOperation("查询所有商品的分类")
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public ResponseEntity<List<Category>> table(){

        return ResponseEntity.ok(categoryService.list());

    }

    @GetMapping("/category/categoryInfo")
    @ApiOperation("查询父id对应的商品，如果没有父id查询所有")
    public ResponseEntity<List<Category>> categoryInfo(Long parentId){

        List<Category> list = categoryService.list(new LambdaQueryWrapper<Category>()
                .eq(ObjectUtil.isNotNull(parentId), Category::getParentId, parentId));

        return ResponseEntity.ok(list);

    }

}
